﻿using System.Windows.Controls;
using Projecturo.Model;
using System.Windows;
using System.Windows.Data;
using System.Collections.Generic;
using Projecturo.ProjecturoClient.ViewModel;
using System.Collections.ObjectModel;

namespace Projecturo.ProjecturoClient.Controls
{
    public class TicketStateMappingControl : Grid
    {
        public ObservableCollection<ViewModelBase> TicketStates
        {
            get { return (ObservableCollection<ViewModelBase>)GetValue(TicketStatesProperty); }
            set { SetValue(TicketStatesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TicketStates.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TicketStatesProperty =
            DependencyProperty.Register("TicketStates", typeof(ObservableCollection<ViewModelBase>), typeof(TicketStateMappingControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(TicketStatesChanged)));


        private static void TicketStatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TicketStateMappingControl stateMappingControl = d as TicketStateMappingControl;
            if (stateMappingControl != null)
            {
                stateMappingControl.Update();
            }
        }

        public IList<TicketStateMapping> StateMappings
        {
            get { return (IList<TicketStateMapping>)GetValue(StateMappingsProperty); }
            set { SetValue(StateMappingsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StateMappings.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StateMappingsProperty =
            DependencyProperty.Register("StateMappings", typeof(IList<TicketStateMapping>), typeof(TicketStateMappingControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(StateMappingsChanged)));

        private static void StateMappingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TicketStateMappingControl stateMappingControl = d as TicketStateMappingControl;
            if (stateMappingControl != null)
            {
                stateMappingControl.Update();
            }
        }

        private List<TextBlock> rowHeaders = new List<TextBlock>();
        private List<TextBlock> colHeaders = new List<TextBlock>();

        private void Update()
        {
            if (TicketStates != null && StateMappings != null)
            {
                BuildGrid();
                BuildCells();
            }
        }

        private void BuildGrid()
        {
            RowDefinitions.Clear();
            ColumnDefinitions.Clear();
            Children.Clear();
            
            for (int i = 0; i <= TicketStates.Count; i++)
            {
                var rowDef = new RowDefinition {Height = GridLength.Auto};
                RowDefinitions.Add(rowDef);

                var colDef = new ColumnDefinition {Width = GridLength.Auto};
                ColumnDefinitions.Add(colDef);
            }
            
            for (int i = 1; i <= TicketStates.Count; i++)
            {
                AddHeader(i, i, 0); // Row
                AddHeader(i, 0, i); // Column;
            }
        }

        private void AddHeader(int ticketStateIndex, int row, int col)
        {
            var block = new TextBlock();

            var textBinding = new Binding
            {
                Source = TicketStates[ticketStateIndex - 1],
                Path = new PropertyPath("Name"),
                Mode = BindingMode.OneWay
            };

            var backGroundBinding = new Binding
                                        {
                                            Source = TicketStates[ticketStateIndex - 1],
                                            Path = new PropertyPath("Color"),
                                            Mode = BindingMode.OneWay
                                        };

            block.SetBinding(TextBlock.TextProperty, textBinding);
            block.SetBinding(TextBlock.BackgroundProperty, backGroundBinding);

            block.SetValue(Grid.RowProperty, row);
            block.SetValue(Grid.ColumnProperty, col);

            if (col == 0)
                colHeaders.Add(block);
            else
                rowHeaders.Add(block);


            Children.Add(block);
        }

        private void BuildCells()
        {
             for (int i = 0; i < StateMappings.Count; i++)
             {
                 TicketStateMapping mapping = StateMappings[i];

                 var box = new CheckBox();

                 var binding = new Binding
                                       {
                                           Source = mapping,
                                           Path = new PropertyPath("IsAllowed"),
                                           Mode = BindingMode.TwoWay
                                       };

                 box.SetBinding(CheckBox.IsCheckedProperty, binding);

                 box.SetValue(Grid.ColumnProperty, GetColumnIndex(mapping.State.Name));
                 box.SetValue(Grid.RowProperty, GetRowIndex(mapping.MappingState.Name));

                 Children.Add(box);
             }
        }

        private int GetRowIndex(string stateName)
        {
            for (int i = 0; i < rowHeaders.Count; i++)
            {
                TextBlock text = rowHeaders[i];
                if (text.Text.Equals(stateName))
                    return i+1;
            }
            return 0;
        }

        private int GetColumnIndex(string stateName)
        {
            for (int i = 0; i < colHeaders.Count; i++)
            {
                TextBlock text = colHeaders[i];
                if (text.Text.Equals(stateName))
                    return i+1;
            }
            return 0;
        }
    }
}
